﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace CustomControlsNS
{
	/// <summary>
	/// Summary description for CustomButton
	/// </summary>
	public class CustomButton : CompositeControl
	{
		private bool bFormatHTML = false;

		public CustomButton()
		{
		}

		public CustomButton(string strText, string strTitle, string strOnClick, string strAccessKey, bool bAppendSpace)
		{
			this.Text = strText;
			this.Title = strTitle;
			this.OnClick = strOnClick;
			this.AccessKey = strAccessKey;
			this.AppendSpace = bAppendSpace;
		}

		public string Text
		{
			get { return (string)ViewState["Text"]; }
			set { ViewState["Text"] = value; }
		}

		public string Title
		{
			get { return (string)ViewState["Title"]; }
			set { ViewState["Title"] = value; }
		}

		public new string AccessKey
		{
			get { return (string)ViewState["AccessKey"]; }
			set { ViewState["AccessKey"] = value; }
		}

		public new bool Enabled
		{
			get
			{
				if (ViewState["Enabled"] == null)
					ViewState["Enabled"] = true;
				return (bool)ViewState["Enabled"];
			}
			set { ViewState["Enabled"] = value; }
		}

		public new string Width
		{
			get { return (string)ViewState["Width"]; }
			set { ViewState["Width"] = value; }
		}

		public bool AppendSpace
		{
			get { return (bool)(this.ViewState["AppendSpace"] ?? false); }
			set { this.ViewState["AppendSpace"] = value; }
		}

		public string OnClick
		{
			get { return (string)ViewState["OnClick"]; }
			set { ViewState["OnClick"] = value; }
		}

		public event EventHandler ServerClick;

		private string GetTabs(int nLevel)
		{
			if (bFormatHTML)
			{
				string s = "\t";
				for (int i = 0; i < nLevel; ++i)
					s += "\t";
				return s;
			}
			else
				return "";
		}

		protected override void CreateChildControls()
		{
			string strEnter = "";
			if (bFormatHTML)
				strEnter = "\n";

			string strButtonHTML = strEnter + "<table border='0' cellpadding='0' cellspacing='0' style='display: inline' id='tbl" + this.ClientID + "' title='" + this.Title + "";
			if (this.Page.Request.Browser.Browser == "IE" && this.AccessKey != null && this.AccessKey != "")
				strButtonHTML += " (ALT + " + this.AccessKey + ")";
			strButtonHTML += "'";
			if (this.Enabled)
			{
				strButtonHTML += " OnClick=\"$get('" + this.ClientID + "_btn').click();\"";
				strButtonHTML += " OnMouseOver=\"HiLightButton('" + this.ClientID + "');\"";
				strButtonHTML += " OnMouseOut=\"RestoreButton('" + this.ClientID + "');\"";
			}
			strButtonHTML += " DoClick=\"$get('" + this.ClientID + "_btn').click();\"";
			strButtonHTML += " DoMouseOver=\"HiLightButton('" + this.ClientID + "');\"";
			strButtonHTML += " DoMouseOut=\"RestoreButton('" + this.ClientID + "');\"";
			strButtonHTML += ">";
			strButtonHTML += strEnter + GetTabs(1) + "<tr height='2'><td colspan='3'></td></tr>";
			strButtonHTML += strEnter + GetTabs(1) + "<tr height='22'>";
			strButtonHTML += strEnter + GetTabs(2) + "<td nowrap class='clsToolbarTextUpL" + (this.Enabled ? "" : "Disabled") + "' width='9'>&nbsp;</td>";
			strButtonHTML += strEnter + GetTabs(2) + "<td nowrap class='clsToolbarTextUpC" + (this.Enabled ? "" : "Disabled") + "' " + (this.Width == "" ? "" : "width='" + this.Width + "'") + ">" + this.Text + "</td>";
			strButtonHTML += strEnter + GetTabs(2) + "<td nowrap class='clsToolbarTextUpR" + (this.Enabled ? "" : "Disabled") + "' width='9'>&nbsp;</td>";
			strButtonHTML += strEnter + GetTabs(1) + "</tr>";
			strButtonHTML += strEnter + GetTabs(1) + "<tr height='2'><td colspan='3'></td></tr>";
			strButtonHTML += strEnter + "</table>";
			if (this.AppendSpace)
				strButtonHTML += strEnter + "&nbsp;";

			HtmlInputButton b = new HtmlInputButton();
			b.ID = "btn";
			if (this.Page.Request.Browser.Browser == "IE")
			{
				b.Style.Add("width", "0px");
				b.Style.Add("height", "0px");
			}
			else
				b.Style.Add("display", "none");
			if (this.AccessKey != null && this.AccessKey != "")
				b.Attributes["AccessKey"] = this.AccessKey;

			if (this.OnClick != null && this.OnClick != "")
			{
				b.Attributes["DoClick"] = this.OnClick;
				if (this.Enabled)
					b.Attributes["OnClick"] = this.OnClick;
			}
			if (!this.Enabled)
				b.Attributes["OnClick"] = "event.returnValue = false;";

			if (this.ServerClick != null)
			{
				b.ServerClick += new EventHandler(OnServerClick);
				b.Attributes["DoClick"] += "; __doPostBack('" + this.UniqueID + "$" + b.ID + "','');";
			}

			Literal l1 = new Literal();
			l1.Text = strButtonHTML;
			Controls.Add(l1);

			Controls.Add(b);

			if (strEnter != "")
			{
				Literal l2 = new Literal();
				l2.Text = "\n";
				Controls.Add(l2);
			}
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			//base.AddAttributesToRender(writer);
		}

		public virtual void OnServerClick(Object sender, EventArgs e)
		{
			if (ServerClick != null)
				ServerClick(sender, e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			AddLinkedStyleSheet(this.Page, "~/Styles/Button.css");
			ScriptManager.RegisterClientScriptInclude(Page, typeof(Page), "Scripts_Button_js", Page.ResolveClientUrl("~/Scripts/Button.js"));

			base.OnPreRender(e);
		}

		public void AddLinkedStyleSheet(Page page, string stylesheet)
		{
			bool bAdded = false;
			foreach (Control c in page.Header.Controls)
				if (c is HtmlLink && ((HtmlLink)c).Href == stylesheet)
				{
					bAdded = true;
					continue;
				}
			if (!bAdded)
			{
				HtmlLink link = new HtmlLink();
				link.Href = stylesheet;
				link.Attributes["rel"] = "stylesheet";
				link.Attributes["type"] = "text/css";

				page.Header.Controls.Add(link);
			}
		}

	}
}
